package GUI;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextField;
import javax.swing.JButton;

import controllers.ClientController;
import javax.swing.ImageIcon;

import entity.UserLoginInfo;

public class RequestUpdatePricesUI extends JFrame {

	private JPanel contentPane;
	private JTextField textpicture;
	private JTextField textSample;
	private JTextField textLayer;
	private JTextField textTime;
	private JTextField textPackage;
	ClientController clientCont;
	String s1,s2,s3,s4,s5;
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					RequestUpdatePricesUI frame = new RequestUpdatePricesUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the Request Update Prices GUI frame.
	 */
	public RequestUpdatePricesUI() {
		setTitle("Request update prices UI");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 629, 365);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JLabel lblWelcomeToRequest = new JLabel("Welcome to Request update prices Window");
		lblWelcomeToRequest.setBounds(108, 5, 259, 14);
		
		JLabel lblService = new JLabel("SERVICE");
		lblService.setBounds(94, 37, 81, 14);
		
		JLabel lblNewPrice = new JLabel("NEW PRICE");
		lblNewPrice.setBounds(266, 37, 86, 14);
		
		JLabel lblPurchasePicture = new JLabel("purchase picture");
		lblPurchasePicture.setBounds(94, 96, 114, 14);
		
		JLabel lblPurchaseSample = new JLabel("purchase sample");
		lblPurchaseSample.setBounds(94, 65, 114, 14);
		
		JLabel lblPurchaseLayer = new JLabel("purchase layer");
		lblPurchaseLayer.setBounds(94, 130, 114, 14);
		
		JLabel lblLocationTimeMember = new JLabel("location time member");
		lblLocationTimeMember.setBounds(94, 161, 132, 14);
		
		JLabel lblPackageMember = new JLabel("package member");
		lblPackageMember.setBounds(94, 184, 114, 14);
		
		textpicture = new JTextField();
		textpicture.setBounds(266, 93, 86, 20);
		textpicture.setColumns(10);
		
		textSample = new JTextField();
		textSample.setBounds(266, 62, 86, 20);
		textSample.setColumns(10);
		
		textLayer = new JTextField();
		textLayer.setBounds(266, 127, 86, 20);
		textLayer.setColumns(10);
		
		textTime = new JTextField();
		textTime.setBounds(266, 158, 86, 20);
		textTime.setColumns(10);
		
		textPackage = new JTextField();
		textPackage.setBounds(266, 184, 86, 20);
		textPackage.setColumns(10);
		
		JButton btnConfirm = new JButton("confirm");
		btnConfirm.setBounds(43, 222, 132, 23);
		btnConfirm.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try{
					btnConfirmActionPerformed(e);
					WorkerGUI frame = new WorkerGUI();
					frame.setVisible(true);
					dispose();
				}catch(IOException e1) {
					e1.printStackTrace();
				}
			}
		});
		
		
		
		
		JButton btnBack = new JButton("back");
		btnBack.setBounds(185, 222, 103, 23);
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				WorkerGUI frame = new WorkerGUI();
				frame.setVisible(true);
				dispose();
			}
		});
		
		
		JButton btnCancle = new JButton("cancel");
		btnCancle.setBounds(298, 222, 114, 23);
		btnCancle.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		contentPane.setLayout(null);
		contentPane.add(lblPurchaseSample);
		contentPane.add(lblService);
		contentPane.add(lblPurchasePicture);
		contentPane.add(lblPurchaseLayer);
		contentPane.add(lblLocationTimeMember);
		contentPane.add(btnConfirm);
		contentPane.add(lblPackageMember);
		contentPane.add(btnBack);
		contentPane.add(lblNewPrice);
		contentPane.add(textSample);
		contentPane.add(textpicture);
		contentPane.add(textPackage);
		contentPane.add(textLayer);
		contentPane.add(textTime);
		contentPane.add(btnCancle);
		contentPane.add(lblWelcomeToRequest);
		
		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setIcon(new ImageIcon("images//sea.jpg"));
		lblNewLabel.setBounds(0, 0, 613, 326);
		contentPane.add(lblNewLabel);
		lblNewLabel.setLayout(null);
	}
	
	/**
	 * handle with the ClientController
	 * connect the update prices with server
	 * @param e
	 * @throws IOException
	 */
public void btnConfirmActionPerformed(ActionEvent e) throws IOException {
		
		ArrayList<String> arr=new ArrayList<String>();
		UserLoginInfo logi=new UserLoginInfo();
		String ip=logi.getIP(); 
		int port=5555;
		this.s1=textSample.getText();
		this.s2=textpicture.getText();
		this.s3=textLayer.getText();
		this.s4=textTime.getText();
		this.s5=textPackage.getText();
		
		if(s1.equals("") & s2.equals("") & s3.equals("") & s4.equals("") & s5.equals("") ){
			JOptionPane.showMessageDialog(null, "Must fill at least one field");
		}
		
		else{
			
			if(s1.equals("")) { s1="0";}
			if(s2.equals("")) { s2="0";}
			if(s3.equals("")) { s3="0";}
			if(s4.equals("")) { s4="0";}
			if(s5.equals("")) { s5="0";}
			
		System.out.println("GUI Request " +s1+"  "+s2+"  "+s3+"  "+s4+"  "+s5 );
		arr.add("RequestUpdatePrices");
		arr.add(s1);
		arr.add(s2);
		arr.add(s3);
		arr.add(s4);
		arr.add(s5);
		
		this.clientCont=new ClientController(ip, port);		
		clientCont.handleMessageFromGui(arr); 
		}
	}

}
